<?php


namespace app\api\controller;


use app\admin\model\UserPintuanLog;
use app\common\controller\Api;
use app\common\model\ScoreLog;

class Score extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    /**
     *
     * 我的积分显示
     */
    public function index()
    {
        $score = $this->auth->score;
        $page = input('page', 1);
        $type = input('type', '');
        $where = [];
        if (!empty($type)) {
            $where['type'] = ['=', $type];
        }
        $score_log = ScoreLog::where(['user_id' => $this->auth->id])
            ->field('id,memo,score,type,status,createtime')
            ->where($where)
            ->page($page, 10)
            ->select();
        $count = ScoreLog::where(['user_id' => $this->auth->id])
            ->where($where)
            ->count();
        $maxpage = ceil($count / 10);
        foreach ($score_log as $v) {
            if ($v['status'] == 1) {
                $v['score'] = '+' . $v['score'];
            } else {
                $v['score'] = '-' . $v['score'];
            }
            $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
        }
        $data = ['score' => $score, 'score_log' => $score_log, 'count' => $count, 'maxpage' => $maxpage];
        $this->success('获取成功', $data);
    }

    /**
     *
     * 我的拼购显示
     */
    public function collage()
    {
        $page = input('page', 1);
        $type = input('type', '');
        $where = [];
        if (!empty($type)) {
            $where['is_win'] = ['=', $type];
        }
        $pinGou_log = UserPintuanLog::where(['user_id' => $this->auth->id])
            ->where('is_win', '<>', -1)
            ->where($where)
            ->field('id,is_win,createtime')
            ->page($page, 10)
            ->select();
        $count = UserPintuanLog::where(['user_id' => $this->auth->id])
            ->where('is_win', '<>', -1)
            ->where($where)
            ->page($page, 10)
            ->count();
        $maxpage = ceil($count / 8);
        foreach ($pinGou_log as &$v) {
            if ($v['is_win'] == 1) {
                $v['win'] = '拼购成功';
            } elseif ($v['is_win'] == 0) {
                $v['win'] = '未中奖';
            }
            $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
            $v->unsetAppend();
            unset($v);
        }
        $data = ['count' => $count, 'maxpage' => $maxpage, 'pinggou' => $pinGou_log];
        $this->success('获取成功', $data);
    }
}